<?php

//power by wshoto.com
global $_W, $_GPC;

$commissionModel = p('commission');

//$openid = $_W['openid'];

$openid = 'os6zfw2YHO6JKeTgHeLDONX2ytOg';

$allMembers = $commissionModel->getAllNextMembers($openid)->getMembers();
$base = $commissionModel->getAllNextMembers($openid, true);

$detail = $base->getAllSalesvolumes()->getAllWealths()->getDetail();

$wealths = $detail->wealthsPrice;
$salesAll = $detail->afterSalesAll;
$member  = $base->member;

if($_GPC['isajax'] == 1){

    if($wealths['unapply'] < 0){
        show_json(0, '很抱歉，您没有可以用来提现的佣金。');
    }

    if($wealths['unapply'] < $base->set['withdraw']){
        show_json(0, '很抱歉，佣金需要满'.$base->set['withdraw'].'元，才可以提现');
    }

    $orderids = array();

    $set = m('common')->getSetData();
    $set = iunserializer($set['sets']);
    $set = $set['trade'];
    $time = time();

    $canApplyTime = $time - $set['refunddays'] * 24 * 60 * 60;
    $unApplyIds = array();
    $unApplyPrice = 0;

    if(!empty($salesAll)){
        foreach($salesAll as $item){
            if($canApplyTime >= $item['finishtime']){
                $orderids[] = array('orderid' => $item['id'], 'level' => $item['level'], 'cprice' => $item['commissionPrice']);
            }else{
                $unApplyIds[] = array('orderid' => $item['id'], 'level' => $item['level'], 'cprice' => $item['commissionPrice']);
                $unApplyPrice += $item['commissionPrice'];
            }
        }
    }

    $newOrderIds = array();
    //申请状态，-2删除，-1无效，0未申请，1申请，2审核通过 3已打款
    foreach($orderids as $o){

        $sql  = 'SELECT `orderid` FROM '.tablename('ewei_shop_order_goods').' WHERE `uniacid` = :uniacid AND `orderid` = :orderid '
              . 'AND `status'.$o['level'].'` = 0';
        $pam = array(':uniacid' => $_W['uniacid'], ':orderid' => $o['orderid']);

        $result = pdo_fetchcolumn($sql, $pam);

        if(!empty($result)){
            $newOrderIds[] = array('orderid' => $o['orderid'], 'level' => $o['level'], 'cprice' => $o['cprice']);
        }
    }

    if(!empty($newOrderIds)){
        foreach ($newOrderIds as $no) {
            pdo_update('ewei_shop_order_goods', array('status' . $no['level'] => 1, 'applytime' . $no['level'] => $time), array('orderid' => $no['orderid'], 'uniacid' => $_W['uniacid']));
        }
    }else{
        show_json(0, '很抱歉，您没有可以用来提现的佣金。');
    }

    $applyno = m('common')->createNO('commission_apply', 'applyno', 'CA');
    $apply = array('uniacid' => $_W['uniacid'], 'applyno' => $applyno, 'orderids' => iserializer($newOrderIds), 'mid' => $member['id'], 'commission' => $wealths['unapply'], 'type' => intval($_GPC['type']), 'status' => 1, 'applytime' => $time);
    pdo_insert('ewei_shop_commission_apply', $apply);
    $returnurl = urlencode($this->createMobileUrl('member/withdraw'));
    $infourl = $this->createMobileUrl('member/info', array('returnurl' => $returnurl));
    $this->model->sendMessage($openid, array('commission' => $commission_ok, 'type' => $apply['type'] == 1 ? '微信' : '余额'), TM_COMMISSION_APPLY);
    show_json(1, '已提交,请等待审核!');
}

//include $this->template('apply');